package com.blog.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blog.entity.Comment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface CommentMapper extends BaseMapper<Comment> {

    // 查询评论并带上用户昵称
    @Select("SELECT c.*, u.nickname FROM comment c LEFT JOIN user u ON c.user_id = u.id WHERE c.article_id = #{articleId} ORDER BY c.create_time DESC")
    List<Comment> findCommentsWithUserByArticleId(@Param("articleId") Long articleId);

    // 查询评论并带上用户昵称，分页
    @Select("SELECT c.*, u.nickname FROM comment c LEFT JOIN user u ON c.user_id = u.id WHERE c.article_id = #{articleId} ORDER BY c.create_time DESC")
    IPage<Comment> findCommentsWithUserByArticleIdPage(IPage<Comment> page, @Param("articleId") Long articleId);

    // 新增方法，用于计算评论总数
    @Select("SELECT COUNT(*) FROM comment WHERE article_id = #{articleId}")
    Long countCommentsByArticleId(@Param("articleId") Long articleId);
} 